package com.efounder.frame.tabbar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.Parcelable;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

public class ChangeColorIconWithTextView extends View {
	/** 保留图片原始颜色 */
	public static final int KEEP_ORIGINAL_COLOR = 0;
	private Bitmap mBitmap;
	private Canvas mCanvas;
	private Paint mPaint;
	/**
	 * 颜色
	 */
	private int mColor = KEEP_ORIGINAL_COLOR;
	private int mSelectedColor = 0xFF00FF00;
	/**
	 * 透明度 0.0-1.0
	 */
	private float mAlpha = 0.0f;
	/**
	 * 图标
	 */
	private Bitmap mIconBitmap;
	/**
	 * 限制绘制icon的范围
	 */
	private Rect mIconRect = new Rect();
	/**
	 * icon底部文本
	 */
	private String mText = "微信";
	private int mTextSize = (int) TypedValue.applyDimension(
			TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics());
	private Paint mTextPaint;
	private Rect mTextBound = new Rect();

//	public ChangeColorIconWithTextView(Context context) {
//		this(context, null, null);
//	}

	/**
	 * 初始化自定义属性值
	 * 
	 * @param context
	 * @param
	 */
	public ChangeColorIconWithTextView(Context context, String text, BitmapDrawable drawable) {
		super(context);

//		BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
		mIconBitmap = drawable.getBitmap();
		mText = text;

		mTextPaint = new Paint();
		mTextPaint.setTextSize(mTextSize);
		mTextPaint.setColor(mColor);
		// 得到text绘制范围
		mTextPaint.getTextBounds(mText, 0, mText.length(), mTextBound);

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		// 得到绘制icon的宽
		int bitmapWidth = Math.min(getMeasuredWidth() - getPaddingLeft()
				- getPaddingRight(), getMeasuredHeight() - getPaddingTop()
				- getPaddingBottom() - mTextBound.height());

		int left = getMeasuredWidth() / 2 - bitmapWidth / 2;
		int top = (getMeasuredHeight() - mTextBound.height()) / 2 - bitmapWidth
				/ 2;
		// 设置icon的绘制范围
		// mIconRect = new Rect(left, top, left + bitmapWidth, top +
		// bitmapWidth);
		mIconRect.left = left;
		mIconRect.top = top;
		mIconRect.right = left + bitmapWidth;
		mIconRect.bottom = top + bitmapWidth;
		Log.i("tag", "---------------------------onGlobalLayout--bitmapWidth:" + bitmapWidth);
		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int alpha = (int) Math.ceil((255 * mAlpha));
		setupSrcBitmap(canvas,alpha);
		setupTargetBitmap(alpha);
		drawSourceText(canvas, alpha);
		drawTargetText(canvas, alpha);
		canvas.drawBitmap(mBitmap, 0, 0, null);
	}

	private void setupSrcBitmap(Canvas canvas, int alpha) {
		mPaint = new Paint();
		if (mColor != 0) {
			mPaint.setColor(mColor);
			mPaint.setAlpha(255 - alpha);
			PorterDuffColorFilter filter = new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_IN);
			mPaint.setColorFilter(filter);
		}
		canvas.drawBitmap(mIconBitmap, null, mIconRect, mPaint);
	}
	private void setupTargetBitmap(int alpha) {
		mBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
				Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		mPaint = new Paint();
		mPaint.setColor(mSelectedColor);
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setAlpha(alpha);
		mCanvas.drawRect(mIconRect, mPaint);
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
		mPaint.setAlpha(255);
		mCanvas.drawBitmap(mIconBitmap, null, mIconRect, mPaint);
	}

	private void drawSourceText(Canvas canvas, int alpha) {
		mTextPaint.setTextSize(mTextSize);
		mTextPaint.setColor(mColor);
		mTextPaint.setAlpha(255 - alpha);
		canvas.drawText(mText, mIconRect.left + mIconRect.width() / 2
				- mTextBound.width() / 2,
				mIconRect.bottom + mTextBound.height(), mTextPaint);
	}

	private void drawTargetText(Canvas canvas, int alpha) {
		mTextPaint.setColor(mSelectedColor);
		mTextPaint.setAlpha(alpha);
		canvas.drawText(mText, mIconRect.left + mIconRect.width() / 2
				- mTextBound.width() / 2,
				mIconRect.bottom + mTextBound.height(), mTextPaint);

	}

	public void setIconAlpha(float alpha) {
		this.mAlpha = alpha;
		invalidateView();
	}

	private void invalidateView() {
		if (Looper.getMainLooper() == Looper.myLooper()) {
			invalidate();
		} else {
			postInvalidate();
		}
	}

	public void setIconColor(int color) {
		this.mColor = color;
		invalidateView();
	}
	public void setSelectedIconColor(int color) {
		this.mSelectedColor = color;
		invalidateView();
	}

	public void setIcon(int resId) {
		this.mIconBitmap = BitmapFactory.decodeResource(getResources(), resId);
		if (mIconRect != null)
			invalidateView();
	}

	public void setIcon(Bitmap iconBitmap) {
		this.mIconBitmap = iconBitmap;
		if (mIconRect != null)
			invalidateView();
	}

	private static final String INSTANCE_STATE = "instance_state";
	private static final String STATE_ALPHA = "state_alpha";

	@Override
	protected Parcelable onSaveInstanceState() {
		Bundle bundle = new Bundle();
		bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
		bundle.putFloat(STATE_ALPHA, mAlpha);
		return bundle;
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state) {
		if (state instanceof Bundle) {
			Bundle bundle = (Bundle) state;
			mAlpha = bundle.getFloat(STATE_ALPHA);
			super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
		} else {
			super.onRestoreInstanceState(state);
		}

	}
	
	public void setText(String text){
		this.mText = text;
		invalidateView();
	}

	public void setTextSize(int textSize){
		this.mTextSize = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_SP, textSize, getResources().getDisplayMetrics());
		invalidateView();
	}
	
	public int getIconWidth(){
		return mIconRect.width();
	}
	
	public int getIconHeight(){
		return mIconRect.height();
	}
}
